package com.example.administrator.szb.activity.myyewu;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.bean.YWGLBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class YWXQActivity_save2 extends MVPBaseActivity {
    private com.example.administrator.szb.view.MyListView listView;
    private EditText editText;
    private Button btn;
    private TextView bar_title;
    private YWGLBean ywglBean;

    private List<YWGLBean.NextBeanX> list = new ArrayList<>();
    private YwAdapter3 adapter;

    private ArrayList<Integer> integerList2 = new ArrayList<>();//二级选中标签
    private ArrayList<Integer> integerList3 = new ArrayList<>();//三级选中标签
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ywxqsave2);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {

        integerList2 = getIntent().getIntegerArrayListExtra("list2");
        integerList3 = getIntent().getIntegerArrayListExtra("list3");
        listView = (com.example.administrator.szb.view.MyListView) findViewById(R.id.listView);
        editText = (EditText) findViewById(R.id.editText);
        btn = (Button) findViewById(R.id.btn);
        bar_title = findViewById(R.id.bar_title);
    }
    YWPopWindow popWindow;
    @Override
    protected void initEvent() {
        ywglBean = (YWGLBean) getIntent().getSerializableExtra("data");
        for (int i=0;i<ywglBean.getNext().size();i++){
            if (integerList2.contains(ywglBean.getNext().get(i).getId())){
                ywglBean.getNext().get(i).setGenre(1);
            }

            for (int j=0;j<ywglBean.getNext().get(i).getNext().size();j++){
                if (integerList3.contains(ywglBean.getNext().get(i).getNext().get(j).getId())){
                    ywglBean.getNext().get(i).getNext().get(j).setGenre(1);
                }
            }
        }



        list.clear();
        list.addAll(ywglBean.getNext());
        adapter = new YwAdapter3(this,list);
        adapter.setIntegerList(integerList2);
        listView.setAdapter(adapter);

        popWindow = new YWPopWindow(this);
        bar_title.setText(ywglBean.getTitle());
        popWindow.setDataBean(ywglBean);
        popWindow.setIntegerList(integerList2);
        popWindow.setOnChangeListener(new YWPopWindow.OnChangeListener() {
            @Override
            public void onChange() {
                ywglBean = popWindow.dataBean;
                list.clear();
                list.addAll(ywglBean.getNext());
                adapter.setIntegerList(popWindow.getIntegerList());
                adapter.notifyDataSetChanged();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWindow.showPopupwindow(R.layout.view_popuwindow_ywxq,R.layout.activity_ywxqsave2,position);;
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str= "";
                //以下数据需要做
                for (int i=0;i<list.size();i++){
                    if (list.get(i).getGenre()==0){
                        DialogUtil.showSimpleDialog(context,"请选择："+list.get(i).getTitle());
                        return;
                    }
                }
                for (int i=0;i<list.size();i++){
                    if (list.get(i).getGenre()==1){
                        str+=list.get(i).getId()+",";
                        for (int j=0;j<list.get(i).getNext().size();j++){
                            if (list.get(i).getNext().get(j).getGenre()==1){
                                str+=list.get(i).getNext().get(j).getId()+",";
                            }
                        }
                    }
                }
                if (TextUtils.isEmpty(str)){
                    finish();
                }else {


                    str+=ywglBean.getId();

                    doSubmit(str);
                }

            }
        });
    }


    @Override
    protected void initData() {

    }

    private void doSubmit(String id_info){
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("business_id", id_info);
        map.put("describe", editText.getText().toString().trim());


        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQSC, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                if (bean.getResult() == 1) {
                    //做返回数据的处理
                    YWNOActivity2.canFinish = true;
                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

}
